home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Compendium Deluxe 2
/
LSD and 17bit Compendium Deluxe - Volume II.iso
/
a
/
prog
/
asmsrc
/
thesource-7.lha
/
Source
/
DefFunc.lha
/
DefFunc
/
y.tab.c
< prev
Wrap
C/C++ Source or Header
|
1993-12-14
|
14KB
|
564 lines
# line 32 "dfcparse.y"
#include <stdio.h>
#include <malloc.h>
#include <math.h>
#include <string.h>
#include "dfctree.h"
#include "dfcsymtable.h"
char* exparserror;
static char ermsgbuff[128];
static Node* yyparsetree;
static int yytreesize;
static Node newnode;
#if NeedFunctionPrototypes
int yyparse(void);
static int yyreverse(void);
#else
extern int yyparse();
extern int yyreverse();
#endif
#if NeedFunctionPrototypes
static int addnode(Node *ptr)
#else
static int addnode(ptr)
Node *ptr;
#endif
{
yytreesize++;
if(yytreesize==1)
{
yyparsetree = (Node*)malloc(sizeof(Node));
}
else
{
yyparsetree = (Node*)realloc((Node*)yyparsetree,
yytreesize*sizeof(Node));
}
if(yyparsetree==0)
{
perror("malloc/realloc in addnode()");
exit(1);
}
if(yyparsetree == 0)
{
fprintf(stderr, "fail to allocate memory in add node\n");
exit(1);
}
/* yyparsetree[yytreesize-1] = *ptr; */
memcpy(yyparsetree+yytreesize-1, ptr, sizeof(Node));
return yytreesize-1;
};
# line 91 "dfcparse.y"
typedef union {
int nodeidx;
int argidx;
double value;
double (*fnctptr)();
char name[32];
} YYSTYPE;
# define CONST 257
# define ARG 258
# define FNCT 259
# define SYM 260
# define SIG 261
#define yyclearin yychar = -1
#define yyerrok yyerrflag = 0
extern int yychar;
extern short yyerrflag;
#ifndef YYMAXDEPTH
#define YYMAXDEPTH 150
#endif
YYSTYPE yylval, yyval;
# define YYERRCODE 256
# line 231 "dfcparse.y"
/* --------------------------------------------------------- */
#include "dfcscan.h"
#if NeedFunctionPrototype
int yyinit(char* expr)
#else
int yyinit(expr)
char *expr;
#endif
{
initargu();
yyexpr = expr;
yyexprlen = strlen(yyexpr);
yypos = 0;
yytreesize = 0;
return 0;
};
#if NeedFunctionPrototypes
static int yyreverse(void)
#else
static int yyreverse()
#endif
/* yyparse() use a LALR(1) bottom-up algorithem to construct the
parse tree. Thus the result tree is upsetdown, i.e. the root
is place on the end of the yyparsetree[]. yyreverse make it
in right order, i.e. yyparsetree[0] be the root */
{
int i;
Node* buff;
if(yytreesize==0) return 0;
buff = (Node*)malloc(sizeof(Node)*yytreesize);
if(buff==0)
{
perror("malloc in reverse()");
exit(1);
}
for(i=0; i<yytreesize; i++) /* reverse */
{
/* buff[i] = yyparsetree[yytreesize-1-i]; */
memcpy(buff+i, yyparsetree+yytreesize-1-i, sizeof(Node));
buff[i].left = yytreesize - 1 - buff[i].left;
buff[i].right= yytreesize - 1 - buff[i].right;
}
for(i=0; i<yytreesize; i++) /* put it back */
{
yyparsetree[i] = buff[i];
}
free(buff);
return yytreesize;
};
#if NeedFunctionPrototypes
int getparsetree(Node* buff)
#else
int getparsetree(buff)
Node* buff;
#endif
/* copy the parse into buff */
{
int i;
for(i=0; i<yytreesize; i++)
{
buff[i] = yyparsetree[i];
}
return yytreesize;
};
#if NeedFunctionPrototypes
static void yyerror(char* s)
#else
static yyerror(s)
char *s;
#endif
{
exparserror=s;
};
#if NeedFunctionPrototypes
static int yywrap(void)
#else
static int yywrap()
#endif
{
return 1;
};
short yyexca[] ={
-1, 1,
0, -1,
-2, 0,
};
# define YYNPROD 22
# define YYLAST 251
short yyact[]={
2, 12, 31, 19, 11, 12, 10, 13, 11, 22,
10, 39, 17, 15, 40, 16, 21, 18, 36, 17,
15, 37, 16, 17, 18, 42, 17, 15, 18, 16,
12, 18, 20, 11, 1, 10, 0, 0, 0, 17,
15, 0, 16, 0, 18, 0, 35, 17, 15, 3,
16, 0, 18, 34, 17, 15, 14, 16, 0, 18,
0, 0, 0, 0, 19, 0, 0, 0, 0, 0,
4, 19, 0, 0, 0, 19, 0, 0, 19, 0,
0, 23, 24, 25, 0, 0, 26, 27, 28, 29,
30, 19, 32, 33, 0, 0, 0, 0, 0, 19,
0, 0, 0, 0, 0, 0, 19, 0, 38, 0,
0, 41, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 6, 8,
9, 7, 6, 8, 9, 7, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 5, 6, 8, 9,
7 };
short yypact[]={
-10,-1000,-1000,-1000, -3, 22, -24,-1000,-1000, -31,
-35, -35, -35,-1000,-1000, -35, -35, -35, -35, -35,
-1000, -39, -35, -91, -91, 12, -19, -19, -91, -91,
-91,-1000, 5, -23,-1000,-1000,-1000, -35, -30,-1000,
-35, -16,-1000 };
short yypgo[]={
0, 70, 34 };
short yyr1[]={
0, 2, 2, 2, 2, 2, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1 };
short yyr2[]={
0, 1, 1, 2, 2, 2, 1, 1, 3, 4,
1, 4, 6, 8, 3, 3, 3, 3, 2, 2,
3, 3 };
short yychk[]={
-1000, -2, 10, 59, -1, 256, 257, 260, 258, 259,
45, 43, 40, 10, 59, 43, 45, 42, 47, 94,
10, 40, 40, -1, -1, -1, -1, -1, -1, -1,
-1, 41, -1, -1, 41, 41, 41, 44, -1, 41,
44, -1, 41 };
short yydef[]={
0, -2, 1, 2, 0, 0, 6, 7, 10, 0,
0, 0, 0, 3, 4, 0, 0, 0, 0, 0,
5, 0, 0, 18, 19, 0, 14, 15, 16, 17,
20, 8, 0, 0, 21, 9, 11, 0, 0, 12,
0, 0, 13 };
#ifndef lint
static char yaccpar_sccsid[] = "@(#)yaccpar 1.6 88/02/08 SMI"; /* from UCB 4.1 83/02/11 */
#endif
#
# define YYFLAG -1000
# define YYERROR goto yyerrlab
# define YYACCEPT return(0)
# define YYABORT return(1)
/* parser for yacc output */
#ifdef YYDEBUG
int yydebug = 0; /* 1 for debugging */
#endif
YYSTYPE yyv[YYMAXDEPTH]; /* where the values are stored */
int yychar = -1; /* current input token number */
int yynerrs = 0; /* number of errors */
short yyerrflag = 0; /* error recovery flag */
yyparse() {
short yys[YYMAXDEPTH];
short yyj, yym;
register YYSTYPE *yypvt;
register short yystate, *yyps, yyn;
register YYSTYPE *yypv;
register short *yyxi;
yystate = 0;
yychar = -1;
yynerrs = 0;
yyerrflag = 0;
yyps= &yys[-1];
yypv= &yyv[-1];
yystack: /* put a state and value onto the stack */
#ifdef YYDEBUG
if( yydebug ) printf( "state %d, char 0%o\n", yystate, yychar );
#endif
if( ++yyps>= &yys[YYMAXDEPTH] ) { yyerror( "yacc stack overflow" ); return(1); }
*yyps = yystate;
++yypv;
*yypv = yyval;
yynewstate:
yyn = yypact[yystate];
if( yyn<= YYFLAG ) goto yydefault; /* simple state */
if( yychar<0 ) if( (yychar=yylex())<0 ) yychar=0;
if( (yyn += yychar)<0 || yyn >= YYLAST ) goto yydefault;
if( yychk[ yyn=yyact[ yyn ] ] == yychar ){ /* valid shift */
yychar = -1;
yyval = yylval;
yystate = yyn;
if( yyerrflag > 0 ) --yyerrflag;
goto yystack;
}
yydefault:
/* default state action */
if( (yyn=yydef[yystate]) == -2 ) {
if( yychar<0 ) if( (yychar=yylex())<0 ) yychar = 0;
/* look through exception table */
for( yyxi=yyexca; (*yyxi!= (-1)) || (yyxi[1]!=yystate) ; yyxi += 2 ) ; /* VOID */
while( *(yyxi+=2) >= 0 ){
if( *yyxi == yychar ) break;
}
if( (yyn = yyxi[1]) < 0 ) return(0); /* accept */
}
if( yyn == 0 ){ /* error */
/* error ... attempt to resume parsing */
switch( yyerrflag ){
case 0: /* brand new error */
yyerror( "syntax error" );
yyerrlab:
++yynerrs;
case 1:
case 2: /* incompletely recovered error ... try again */
yyerrflag = 3;
/* find a state where "error" is a legal shift action */
while ( yyps >= yys ) {
yyn = yypact[*yyps] + YYERRCODE;
if( yyn>= 0 && yyn < YYLAST && yychk[yyact[yyn]] == YYERRCODE ){
yystate = yyact[yyn]; /* simulate a shift of "error" */
goto yystack;
}
yyn = yypact[*yyps];
/* the current yyps has no shift onn "error", pop stack */
#ifdef YYDEBUG
if( yydebug ) printf( "error recovery pops state %d, uncovers %d\n", *yyps, yyps[-1] );
#endif
--yyps;
--yypv;
}
/* there is no state on the stack with an error shift ... abort */
yyabort:
return(1);
case 3: /* no shift yet; clobber input char */
#ifdef YYDEBUG
if( yydebug ) printf( "error recovery discards char %d\n", yychar );
#endif
if( yychar == 0 ) goto yyabort; /* don't discard EOF, quit */
yychar = -1;
goto yynewstate; /* try again in the same state */
}
}
/* reduction by production yyn */
#ifdef YYDEBUG
if( yydebug ) printf("reduce %d\n",yyn);
#endif
yyps -= yyr2[yyn];
yypvt = yypv;
yypv -= yyr2[yyn];
yyval = yypv[1];
yym=yyn;
/* consult goto table to find next state */
yyn = yyr1[yyn];
yyj = yypgo[yyn] + *yyps + 1;
if( yyj>=YYLAST || yychk[ yystate = yyact[yyj] ] != -yyn ) yystate = yyact[yypgo[yyn]];
switch(yym){
case 1:
# line 111 "dfcparse.y"
{
return 0;
} break;
case 2:
# line 114 "dfcparse.y"
{
return 0;
} break;
case 3:
# line 117 "dfcparse.y"
{
return yyreverse();
} break;
case 4:
# line 120 "dfcparse.y"
{
return yyreverse();
} break;
case 5:
# line 123 "dfcparse.y"
{
return -1;
} break;
case 6:
# line 128 "dfcparse.y"
{
newnode.type = const_node;
newnode.content.value = yypvt[-0].value;
yyval.nodeidx = addnode(&newnode);
} break;
case 7:
# line 134 "dfcparse.y"
{
sprintf(ermsgbuff,
"unknow token \"%s\"", yypvt[-0].name);
exparserror = ermsgbuff;
return -1;
} break;
case 8:
# line 140 "dfcparse.y"
{
newnode.type = const_node;
newnode.content.value = yypvt[-2].value;
yyval.nodeidx = addnode(&newnode);
} break;
case 9:
# line 146 "dfcparse.y"
{
newnode.type = const_node;
newnode.content.value = yypvt[-3].value;
yyval.nodeidx = addnode(&newnode);
} break;
case 10:
# line 152 "dfcparse.y"
{
newnode.type = arg_node;
newnode.content.argidx = yypvt[-0].argidx;
yyval.nodeidx = addnode(&newnode);
} break;
case 11:
# line 157 "dfcparse.y"
{
newnode.type = simplex_fnct_node;
newnode.content.fnctptr = yypvt[-3].fnctptr;
newnode.right = yypvt[-1].nodeidx;
yyval.nodeidx = addnode(&newnode);
} break;
case 12:
# line 164 "dfcparse.y"
{
newnode.type = duplex_fnct_node;
newnode.content.fnctptr = yypvt[-5].fnctptr;
newnode.left = yypvt[-3].nodeidx;
newnode.right= yypvt[-1].nodeidx;
yyval.nodeidx = addnode(&newnode);
} break;
case 13:
# line 172 "dfcparse.y"
{
exparserror
= "not support triplex function yet";
return -1;
} break;
case 14:
# line 177 "dfcparse.y"
{
newnode.type = binary_op_node;
newnode.content.op = op_sum;
newnode.left = yypvt[-2].nodeidx;
newnode.right= yypvt[-0].nodeidx;
yyval.nodeidx = addnode(&newnode);
} break;
case 15:
# line 185 "dfcparse.y"
{
newnode.type = binary_op_node;
newnode.content.op = op_sub;
newnode.left = yypvt[-2].nodeidx;
newnode.right= yypvt[-0].nodeidx;
yyval.nodeidx = addnode(&newnode);
} break;
case 16:
# line 193 "dfcparse.y"
{
newnode.type = binary_op_node;
newnode.content.op = op_mul;
newnode.left = yypvt[-2].nodeidx;
newnode.right= yypvt[-0].nodeidx;
yyval.nodeidx = addnode(&newnode);
} break;
case 17:
# line 201 "dfcparse.y"
{
newnode.type = binary_op_node;
newnode.content.op = op_div;
newnode.left =yypvt[-2].nodeidx;
newnode.right=yypvt[-0].nodeidx;
yyval.nodeidx = addnode(&newnode);
} break;
case 18:
# line 209 "dfcparse.y"
{
newnode.type = unary_op_node;
newnode.content.op = op_neg;
newnode.right = yypvt[-0].nodeidx;
yyval.nodeidx = addnode(&newnode);
} break;
case 19:
# line 216 "dfcparse.y"
{
yyval.nodeidx = yypvt[-0].nodeidx;
} break;
case 20:
# line 219 "dfcparse.y"
{
newnode.type = duplex_fnct_node;
newnode.content.fnctptr = pow;
newnode.left = yypvt[-2].nodeidx;
newnode.right= yypvt[-0].nodeidx;
yyval.nodeidx = addnode(&newnode);
} break;
case 21:
# line 227 "dfcparse.y"
{
yyval.nodeidx = yypvt[-1].nodeidx;
} break;
}
goto yystack; /* stack new state and value */
}